﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace ThePingMasterConfiguration.Validation
{
    class IntegerValidationRule : ValidationRule
    {
        private const string Message = "Must be an integer between {0} and {1}";

        private int _min = int.MinValue;
        private int _max = int.MaxValue;

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            ValidationResult result; 
            try
            {
                int val = int.Parse(value.ToString());
                if (val >= Min && val <= Max)
                    result = new ValidationResult(true, null);
                else
                {
                    result = new ValidationResult(false, String.Format(Message, Min, Max));
                }
            }
            catch (Exception)
            {
                result = new ValidationResult(false, String.Format(Message, Min, Max));
            }
            return result;
        }

        public int Min { get { return _min; } set { _min = value; } }

        public int Max { get { return _max; } set { _max = value; } }
    }
}
